package com.loyal.mwm.handler;

import android.text.TextUtils;
import android.view.View;

import com.loyal.client.impl.RxSubscriberListener;
import com.base.kit.DevUtil;
import com.base.kit.GsonUtil;
import com.base.kit.ResUtil;
import com.loyal.mwm.R;
import com.loyal.mwm.base.BaseClickHandler;
import com.loyal.mwm.databinding.ActivityAccountBinding;
import com.loyal.mwm.libs.rxjava.RxProgressSubscriber;
import com.loyal.mwm.ui.AccountSafetyActivity;
import com.loyal.rx3.RxUtil;
import com.loyal.mwm.beans.ObservableAccountBean;
import com.loyal.mwm.beans.ResultBean;
import com.loyal.mwm.impl.ServerImpl;

public class AccountSafetyHandler extends BaseClickHandler<ActivityAccountBinding> implements RxSubscriberListener<String> {

    public AccountSafetyHandler(AccountSafetyActivity activity, ActivityAccountBinding binding) {
        super(activity, binding);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.account_mm_reset:
                hasIntentParams(true);
                //activity.startActivityForResultByAct(RegisterActivity.class, IntImpl.reqCodeUpdateMM);
                break;
            case R.id.account_device_lock:
                String des = binding.accountDeviceLock.getContentDescription().toString();
                ObservableAccountBean observableAccountBean = new ObservableAccountBean();
                observableAccountBean.username.set(activity.getUsername());
                observableAccountBean.locked.set(TextUtils.equals("off", ResUtil.replaceText(des)) ? "1" : "0");
                observableAccountBean.device.set(ServerImpl.deviceId());
                observableAccountBean.serial.set(DevUtil.serial());
                RxProgressSubscriber<String> subscriber = new RxProgressSubscriber<>(activity);
                subscriber.setDialogMessage("").showProgressDialog(true);
                subscriber.setSubscribeListener(this);
                RxUtil.rxExecute(subscriber.accountUpdate(observableAccountBean.toString()), subscriber);
                break;
            case R.id.account_destroy:
                //intentBuilder.putExtra("extra", "destroy");
                //activity.startActivityForResultByAct(RegisterActivity.class, IntImpl.reqCodeDestroy);
                break;
        }
    }

    @Override
    public void onResult(int what, Object tag, String result) {
        ResultBean<?> resultBean = GsonUtil.json2Bean(result, ResultBean.class);
        if (null != resultBean) {
            if (TextUtils.equals("1", resultBean.getCode())) {
                String des = binding.accountDeviceLock.getContentDescription().toString();
                if (TextUtils.equals("off", des)) {
                    activity.showToast("该账号已与此设备绑定");
                    binding.accountDeviceLock.setContentDescription(getString(R.string.on));
                    binding.accountDeviceLock.setImageResource(R.mipmap.switch_on);
                } else {
                    activity.showToast("已取消与此设备绑定");
                    binding.accountDeviceLock.setImageResource(R.mipmap.switch_off);
                    binding.accountDeviceLock.setContentDescription(getString(R.string.off));
                }
            } else activity.showToast(resultBean.getMessage());
        } else activity.showToast("解析设备锁信息失败");
    }

    @Override
    public void onError(int what, Object tag, Throwable e) {
        activity.showErrorDialog("", e);
    }
}
